package com.oxbix.spanlogistics.wxapi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.kye.express.R;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.event.EventFactory;
import com.oxbix.spanlogistics.share.Contants;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private IWXAPI mWXApi;
    private static final String TENCENT_WECHAT_APPID = Contants.TENCENT_WECHAT_APPID;
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        if (mWXApi == null) {
            mWXApi = WXAPIFactory.createWXAPI(this, TENCENT_WECHAT_APPID, true);
            mWXApi.registerApp(TENCENT_WECHAT_APPID);
            mWXApi.handleIntent(getIntent(), this);
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {
        SendMessageToWX.Req req = (SendMessageToWX.Req) baseReq;
        flag = req.scene;
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int result = 0;
        int type = baseResp.getType();
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = R.string.wechat_pay_success;
                break;
            case BaseResp.ErrCode.ERR_COMM:
                result = R.string.wechat_pay_fail;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.wechat_pay_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.wechat_pay_fail;
                break;
            default:
                result = R.string.wechat_pay_fail;
                break;
        }
//        ToastManager.show(this, result);
        EventFactory.OnWechatPayEvent onWechatPayEvent = new EventFactory.OnWechatPayEvent();
        onWechatPayEvent.code = baseResp.errCode;
        onWechatPayEvent.msg = getResources().getString(result);
        EventBus.getDefault().post(onWechatPayEvent);
        finish();
    }

}
